package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.Company;
import com.itheima.service.CompanyListService;
import com.itheima.utils.QiniuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.JedisPool;

import java.io.IOException;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/companylist")
public class CompanyListController {
    @Reference
    private CompanyListService companyListService;
    @Autowired
    private JedisPool jedisPool;

    //查找页面
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult = companyListService.pageQuery(queryPageBean);
        return pageResult;
    }
//添加公司地址
    @RequestMapping("/add")
    public Result add(@RequestBody Company company){
        //查询是否重复添加
        try{
            Company byAddress = companyListService.findAllAddress(company.getCompanyAddress());
            if (byAddress==null) {
                companyListService.add(company);
                return new Result(true, MessageConstant.ADD_COMPANY_ADDRESS_SUCCESS);//查询成功
            }else {
                return new Result(false, MessageConstant.ADD_COMPANY_ADDRESS_REPEAT);
            }
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_COMPANY_ADDRESS_FAIL);//查询失败
        }
    }
//删除公司地址
    @RequestMapping("/delete")
    public Result delete(Integer id){
        try{
            companyListService.delete(id);
            return new Result(true, "删除记录成功");//查询成功
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"删除记录失败");//查询失败
        }
    }
//查询所有
    @RequestMapping("/findAll")
    public Result findAll(){
        try{
            List<Company> list = companyListService.findAll();
            return new Result(true, "查询所有地址成功",list);//查询成功
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"查询所有地址失败");//查询失败
        }
    }

    /**
     * 上传图片
     * @param multipartFile
     * @return
     */
    @RequestMapping("/upload")
    public Result upload(@RequestParam("imgFile") MultipartFile multipartFile) {
        try {
            //获取原始文件名
            String originalImagname = multipartFile.getOriginalFilename();
            //获取文件后缀名的起始索引
            int index = originalImagname.lastIndexOf(".");
            String suffix = originalImagname.substring(index);
            //使用UUID产生随机的文件名，防止文件名重复
            String fileName = UUID.randomUUID().toString() + suffix;
            QiniuUtils.upload2Qiniu(multipartFile.getBytes(), fileName);
            //将上传的图片名称存入redis,基于redis的set集合存储
            jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_RESOURCES,fileName);
            //文件上传成功
            return new Result(true, MessageConstant.PIC_UPLOAD_SUCCESS,fileName);
        } catch (IOException e) {
            e.printStackTrace();
            //文件上传失败
            return new Result(false,MessageConstant.PIC_UPLOAD_FAIL);
        }
    }


}
